
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package doghost.controllers;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.RollbackException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import doghost.DoghostApp;
import doghost.editors.DiariaEditor;
import doghost.entities.Diaria;

/**
 *
 * @author duo
 */
public class DiariaController {
    protected Diaria diaria;
    protected boolean operationOk;

    public Diaria getDiaria() {
        return diaria;
    }

    public void setDiaria(Diaria diaria) {
        this.diaria = diaria;
    }

    /**
     * Get the value of operationOk
     *
     * @return the value of operationOk
     */
    public boolean isOperationOk() {
        return operationOk;
    }

    /**
     * Set the value of operationOk
     *
     * @param operationOk new value of operationOk
     */
    public void setOperationOk(boolean operationOk) {
        this.operationOk = operationOk;
    }

    public void newDiaria(EntityManager entityManager, List list) {
        // create a transient object
        this.setOperationOk(false);
        setDiaria(new Diaria());
        JFrame mainFrame = DoghostApp.getApplication().getMainFrame();
        DiariaEditor diariaEditor = new DiariaEditor(mainFrame, true);
        diariaEditor.setLocationRelativeTo(mainFrame);
        diariaEditor.setCurrentRecord(getDiaria());
        diariaEditor.setVisible(true);
        if (diariaEditor.isSaveFired()) {
            try {
                entityManager.getTransaction().begin();
                entityManager.persist(getDiaria());
                entityManager.getTransaction().commit();
                this.setOperationOk(true);
                if(list != null) {
                    list.add(getDiaria());
                }
                Logger.getLogger(this.getClass().getName()).
                        log(Level.INFO, "Diaria adicionado(a) com sucesso: " +
                        getDiaria());
            }
            catch (Exception e) {
                Logger.getLogger(this.getClass().getName()).
                        log(Level.SEVERE, "Erro adicionando Diaria: " + e);
            }
        }
    }

    public void editDiaria(EntityManager entityManager, List list, int index) {
        this.setOperationOk(false);
        entityManager.getTransaction().begin();
        JFrame mainFrame = DoghostApp.getApplication().getMainFrame();
        DiariaEditor diariaEditor = new DiariaEditor(mainFrame, true);
        diariaEditor.setLocationRelativeTo(mainFrame);
        diariaEditor.setCurrentRecord(getDiaria());
        diariaEditor.setVisible(true);
        if (diariaEditor.isSaveFired()) {
            try {
                entityManager.merge(getDiaria());
                entityManager.getTransaction().commit();
                this.setOperationOk(true);
                Logger.getLogger(this.getClass().getName()).
                    log(Level.INFO, "Diaria Editado(a) com sucesso: " +
                    getDiaria());
            } catch (RollbackException rex) {
                // refresh diaria in list
                Logger.getLogger(this.getClass().getName()).
                    log(Level.SEVERE, "Diaria erro de edição (rollback) : " +
                    rex);
                Diaria originalDiaria = (Diaria)
                        entityManager.find(getDiaria().getClass(),
                        getDiaria().getId());
                if(list != null) {
                    list.remove(getDiaria());
                    list.add(index, originalDiaria);
                }
            } catch (Exception e) {
                Logger.getLogger(this.getClass().getName()).
                        log(Level.SEVERE, "Diaria erro de edição: " + e);
                if (entityManager.getTransaction().isActive()) {
                    try {
                        entityManager.getTransaction().rollback();
                    } catch (Exception erb) {
                        Logger.getLogger(this.getClass().getName()).
                                log(Level.SEVERE, "Erro abandonando edição de diaria, " +
                                "transação ativa: " + erb);
                    }
                }
            }
        } else {
            try {
                if (entityManager.getTransaction().isActive()) {
                    entityManager.refresh(getDiaria());
                    entityManager.getTransaction().commit();
                }
                // refresh diaria in list
                Diaria originalDiaria = (Diaria)
                        entityManager.find(getDiaria().getClass(),
                        getDiaria().getId());
                if(list != null) {
                    list.remove(getDiaria());
                    list.add(index, originalDiaria);
                }
                Logger.getLogger(this.getClass().getName()).
                    log(Level.INFO, " Edição de Diaria cancelada com sucesso " +
                    "diaria: " + getDiaria());
            } catch (RollbackException rex) {
                Logger.getLogger(this.getClass().getName()).
                    log(Level.SEVERE, "Erro cancelando edição de diaria " +
                    "(rollback) : " + rex);
                if (entityManager.getTransaction().isActive()) {
                    try {
                        entityManager.getTransaction().rollback();
                    } catch (Exception erb) {
                        Logger.getLogger(this.getClass().getName()).
                                log(Level.SEVERE, "Erro abandonando eliminação de diaria. " +
                                "transação ativa: " + erb);
                    }
                }
            }
        }
    }

    public void deleteDiaria(EntityManager entityManager, List list, int index) {
        this.setOperationOk(false);
        Object[] options = {"Sim", "Não"};
        int n = JOptionPane.showOptionDialog(null, "Remove a linha ?", "Aviso",
                JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, null);
        if (n ==JOptionPane.YES_OPTION) {
            try {
                entityManager.getTransaction().begin();
                entityManager.remove(getDiaria());
                entityManager.getTransaction().commit();
                this.setOperationOk(true);
                if(list != null) {
                    list.remove(getDiaria());
                }
            } catch (Exception e) {
                Logger.getLogger(this.getClass().getName()).
                    log(Level.SEVERE, "Erro eliminando diaria: " + e);
            }
        }
    }


}

